package com.nononsenseapps.feeder.util;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.corpus.tag.Nature;
import com.hankcs.hanlp.seg.common.Term;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import timber.log.Timber;


public class NlpUtils {

    public static Nature[] EXP_NATURE={Nature.an,Nature.vn,Nature.l,Nature.i,Nature.j,Nature.gi,Nature.gg,Nature.gbc,Nature.gc,Nature.gp,Nature.gm,Nature.g
             ,Nature.nz,Nature.nbp,Nature.nbc,Nature.nba,Nature.nb,Nature.nmc,Nature.nm
             ,Nature.nic,Nature.nit,Nature.ni
             ,Nature.nf,Nature.ng,Nature.nnd,Nature.nnt,Nature.nn,Nature.nhd,Nature.nhm
             ,Nature.nh,Nature.nth,Nature.nts,Nature.ntu,Nature.nto,Nature.ntch,Nature.ntcb,Nature.ntcf,Nature.ntc,Nature.nt
             ,Nature.nsf,Nature.ns,Nature.nr,Nature.nrj,Nature.nrf,Nature.n,Nature.nl,Nature.nx
      };

    public static List<String> buildTags(String titles){
        ArrayList<String> words =new ArrayList<>();
        List<Term> wordList=HanLP.segment(titles);
        for(Term term:wordList){
            //Timber.d(term.toString());
            for(Nature nature:EXP_NATURE)
                if(nature==term.nature) {
                    words.add(term.word);
                    break;
                }
        }
        //Timber.d("build tags:"+words.toString());
        return words;
    }

    public static List<String> buildSummary(String doc){
        return HanLP.extractSummary(doc,5);
    }

    public static String tranLanguage(Locale from,Locale to,String textStr){
        Timber.d("tranLanguage from:%s,to:%s",from.toString(),to.toString());
        if (Locale.CHINESE.equals(from) || Locale.SIMPLIFIED_CHINESE.equals(from)) {
            if (CHINA_HK.equals(to)) {
                return HanLP.s2hk(textStr);
            } else if (Locale.TRADITIONAL_CHINESE.equals(to)) {
                return HanLP.s2tw(textStr);
            }
            return textStr;
        } else {
            final boolean b = Locale.SIMPLIFIED_CHINESE.equals(to) || Locale.CHINESE.equals(to);
            if (Locale.TRADITIONAL_CHINESE.equals(from)) {
                if (b) {
                    return HanLP.tw2s(textStr);
                } else if (CHINA_HK.equals(to)) {
                    return HanLP.tw2hk(textStr);
                }
                return textStr;
            } else if (CHINA_HK.equals(from)) {
                if (b) {
                    return HanLP.hk2s(textStr);
                } else if (Locale.TRADITIONAL_CHINESE.equals(to)) {
                    return HanLP.hk2tw(textStr);
                }
                return textStr;
            }
        }
        return textStr;
    }

    public static Locale CHINA_HK=new Locale("cn","zh","HK");
}
